package org.controller
{
	import flash.events.Event;
	
	import mx.rpc.AsyncResponder;
	import mx.rpc.AsyncToken;
	import mx.rpc.http.HTTPService;
	
	import org.model.Model;
	import org.utils.Utils;
	
	
	public class Controller
	{
		public function Controller()
		{
		}
		
		
		// ------------------------------- ENQUEUE
		public static function addTrack(item:Object):void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL+"?add=1";
			service.method = "POST";
			service.contentType = "application/xml;charset=UTF-8";
			
			var tracklist:XML = <tracklist />;
			
			tracklist = Utils.getXMLtracklist(item);
			
			trace(tracklist.toXMLString());
			var token:AsyncToken = service.send(tracklist);
			token.addResponder(new AsyncResponder(addTrackSuccess, addTrackFault));
		}
		
		private static function addTrackSuccess(result:Object, token:Object = null):void
		{
			trace("OK - "+result.statusCode);
		}
		
		private static function addTrackFault(result:Object, token:Object = null):void
		{
			trace("FAIL - "+result.statusCode);
		}
		
		
		// ------------------------------- REFRESH
		public static function refresh(event:Event):void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL;
			service.method = "GET";
			var token:AsyncToken = service.send();
			token.addResponder(new AsyncResponder(refreshSuccess, refreshFault));
		}
		
		private static function refreshSuccess(result:Object, token:Object = null):void
		{
			if(!Model.refreshBlocked || Model.remotePlaylist == null){
				if(result.result.status.playlist != null){
					Model.remotePlaylist = result.result.status.playlist.item; 
				}else{
					Model.remotePlaylist = null;
				}
			}
			
			if(!Model.editingRemoteDir){
				Model.remoteDirectory = String(result.result.status.path);
			}
			
			Model.status = -1;
			Model.status = parseInt(result.result.status.status);
			Model.songName = null;
			Model.songName = String(result.result.status.song);
			
			Model.volume = parseInt(result.result.status.volume);
			Model.progress = parseInt(result.result.status.progress);
			
			if(!Model.remoteStructureInitialized){
				FileBrowserController.loadRemoteStructure();
				Model.remoteStructureInitialized = true;
			}
				
		}
		
		private static function refreshFault(result:Object, token:Object = null):void
		{
			trace("Chyba komunikace - "+result.statusCode+" - "+Model.PLAYER_SERVICE_URL);
		}
		
		
		// ----------------------------- PLAY
		public static function play():void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL;
			service.method = "POST";
			var token:AsyncToken = service.send({control : "play"});
			token.addResponder(new AsyncResponder(controlSuccess, controlFault));
		}
		
		
		// ----------------------------- PAUSE
		public static function pause():void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL;
			service.method = "POST";
			var token:AsyncToken = service.send({control : "pause"});
			token.addResponder(new AsyncResponder(controlSuccess, controlFault));
		}
		
		// ----------------------------- NEXT
		public static function playNext():void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL;
			service.method = "POST";
			var token:AsyncToken = service.send({control : "next"});
			token.addResponder(new AsyncResponder(controlSuccess, controlFault));
		}
		
		// ----------------------------- STOP
		public static function stop():void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL;
			service.method = "POST";
			var token:AsyncToken = service.send({control : "stop"});
			token.addResponder(new AsyncResponder(controlSuccess, controlFault));
		}
		
		// ----------------------------- VOLUME
		public static function volume(value:int):void
		{
			var service:HTTPService = new HTTPService();
			service.url = Model.PLAYER_SERVICE_URL;
			service.method = "POST";
			var token:AsyncToken = service.send({volume : value});
			token.addResponder(new AsyncResponder(controlSuccess, controlFault));
		}
		
		private static function controlSuccess(result:Object, token:Object = null):void
		{
			trace("OK - "+result.statusCode);
		}
		
		private static function controlFault(result:Object, token:Object = null):void
		{
			trace("FAIL - "+result.statusCode);
		}
		
		
	}
}